package marksys.free.com.marksys.ui.common;

import android.databinding.ViewDataBinding;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import marksys.free.com.marksys.utils.UnboxUtils;


/**
 * 默认有更多数据就显示FooterView，没有就不显示FooterView
 * showAlways == true 时。一直显示FooterView
 *
 * @author yawei
 * @data on 2018/8/3 下午12:33
 * @email zyawei@live.com
 */
public abstract class BaseLoadMoreAdapter<T, V extends ViewDataBinding> extends BaseFooterListAdapter<T, V> {


    private OnLoadMoreListener loadMoreListener;


    @Override
    protected final void bindingFooter(ViewDataBinding binding, Object data) {
        bindingFooter(binding, hasMoreData, data);
    }

    private RecyclerView recyclerView;

    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.recyclerView = recyclerView;
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                    int lastPosition = layoutManager.findLastVisibleItemPosition();
                    if (lastPosition == getItemCount() - 1 && null != loadMoreListener && hasMoreData) {
                        loadMoreListener.onLoadMore();
                    }
                }
            }
        });
        registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
            @Override
            public void onItemRangeInserted(int positionStart, int itemCount) {
                super.onItemRangeInserted(positionStart, itemCount);
                if (positionStart == 0) {
                    loadMoreListener.onLoadMore();
                    //recyclerView.smoothScrollToPosition(0);
                }
            }
        });
    }

    @Override
    public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        this.recyclerView = null;
    }

    public void setOnLoadMoreListener(OnLoadMoreListener loadMoreListener) {
        this.loadMoreListener = loadMoreListener;
    }


    private boolean hasMoreData = false;
    private boolean showNoMoreView = false;

    public final void showNoMoreView(boolean show) {
        this.showNoMoreView = show;
    }

    public final void hasMoreData(Boolean hasMore) {
        this.hasMoreData = UnboxUtils.safeUnBox(hasMore);
        if (null != recyclerView &&( recyclerView.isAnimating() || recyclerView.isComputingLayout())) {
            return;
        }
        showFooterView(showNoMoreView || hasMoreData);
    }

    public final void hasMoreData(boolean hasMore, Object data) {
        this.hasMoreData = UnboxUtils.safeUnBox(hasMore);
        if (null != recyclerView &&( recyclerView.isAnimating() || recyclerView.isComputingLayout())) {
            return;
        }
        showFooterView(showNoMoreView || hasMoreData, data);
    }

    protected abstract void bindingFooter(ViewDataBinding binding, boolean hasMoreData, Object data);


    public interface OnLoadMoreListener {
        void onLoadMore();
    }

}
